package com.demo.travel.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.demo.travel.R;
import com.demo.travel.base.BaseActivity;
import com.demo.travel.bean.UserBean;
import com.demo.travel.db.UserDao;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.numEt)
    EditText numEt;

    @BindView(R.id.passEt)
    EditText passEt;

    @BindView(R.id.pass1Et)
    EditText pass1Et;

    @BindView(R.id.backIv)
    ImageView backIv;

    private UserDao userDao;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        backIv.setVisibility(View.VISIBLE);
        userDao = new UserDao(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public String getActivityTitle() {
        return "注册";
    }

    @OnClick({R.id.regTv, R.id.backIv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.regTv:
                reg();
                break;
            case R.id.backIv:
                finish();
                break;
        }
    }

    private void reg() {
        if (TextUtils.isEmpty(numEt.getText()) ||
                TextUtils.isEmpty(passEt.getText()) ||
                TextUtils.isEmpty(pass1Et.getText())
        ) {
            showToast("输入内容不能为空！");
            return;
        }
        if (!pass1Et.getText().toString().equals(passEt.getText().toString())) {
            showToast("两次输入的密码不一致！");
            return;
        }

        if (userDao.userExist(numEt.getText().toString())) {
            showToast("用户已存在！");
            return;
        }

        UserBean userBean = new UserBean();
        userBean.setUsername(numEt.getText().toString());
        userBean.setPassword(passEt.getText().toString());
        userDao.insert(userBean);
        showToast("注册成功！请登录...");
        finish();
    }


}
